package com.jn.exam.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jn.exam.DateTimeUtil;
import com.jn.exam.HTMLUtil;
import com.jn.exam.R;
import com.jn.exam.question.entity.Question;
import com.jn.exam.question.entity.vo.QuestionWrapper;
import com.jn.exam.question.mapper.QuestionMapper;
import com.jn.exam.question.service.OssFeignService;
import com.jn.exam.question.service.QuestionService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Future;

/**
 * <p>
 * 总题库 前端控制器
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-01-17
 * <p>
 * 管理员操作总题库
 */
@RestController
@RequestMapping("/question/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private OssFeignService ossFeignService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //获取所有题目
    @GetMapping("getAllQuestion")
    public R getAllQuestion() {
        List<Question> questions = questionService.list(null);
        return R.ok().data("questions", questions);
    }


    //分页查询
    @ApiOperation("模糊分页查")
    @PostMapping("pageQuestionCondition")
    public R pageQuestionCondition(@RequestBody QuestionWrapper questionWrapper) {
        Integer total = questionService.getPageTotal(questionWrapper);
        if (total == 0) {
            return R.ok().data("rows", null).data("total", total);
        }
        long current = questionWrapper.getCurrent();
        long limit = questionWrapper.getLimit();
        questionWrapper.setCurrent(current * limit);
//        if (current * limit + limit - 1 > total) {
//            questionWrapper.setLimit(total - 1);
//        }
        List<Question> questions = questionService.pageSelect(questionWrapper);
        if (questions != null) {
            return R.ok().data("rows", questions).data("total", total);
        }
        return R.error().message("查询失败");
    }

    //管理员添加题到总题库
    @ApiOperation("管理员添加题到总题库")
    @PutMapping(value = "addQuestionBody")
    public R addQuestion(@RequestBody Question question) {
        boolean isSave = questionService.save(question);
        if (isSave) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    //更新题目
    @ApiOperation("更新题目")
    @PostMapping("updateQuestion")
    public R updateQuestion(@RequestBody Question question) {
        boolean isUpdate = questionService.updateById(question);
        if (isUpdate) {
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }


    //逻辑删除多个
    @ApiOperation("逻辑删除多个")
    @PostMapping("deleteByIdsLogic")
    @Transactional
    public R deleteByIdsLogic(String[] ids) {
        boolean removeByIds = questionService.removeByIds(ids);
        if (removeByIds) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    //彻底删除多个或单个
    @ApiOperation("彻底删除多个")
    @PostMapping("completeDeleteByIds")
    public R completeDeleteByIds(String[] ids) throws Exception {
        List<Future<Boolean>> futures = new ArrayList<>();
        for (String id : ids) {
            Future<Boolean> future = questionService.completeDeleteById(id);
            futures.add(future);
        }
        for (Future<Boolean> future : futures) {
            Boolean b = future.get();
            if (!b) {
                return R.error().message("删除失败");
            }
        }
        return R.ok().message("删除成功");
    }

    @ApiOperation("查询已删除")
    @GetMapping("getDeleted")
    public R getDeleted() {
        List<Question> questions = questionService.getDeletedQuestions();
        if (questions != null) {
            for (Question question : questions) {
                Date d1 = question.getGmtModified();
                Date d2 = new Date();
                int daysBetweenTwoDate = DateTimeUtil.daysBetweenTwoDate(d1, d2);
                question.setRemainingDays(daysBetweenTwoDate);
            }
            return R.ok().data("questions", questions);
        }
        return R.error().message("查询失败");
    }

    @ApiOperation("恢复已删除")
    @PostMapping("recoveryDeleted")
    public R recoveryDeleted(String[] ids) {
        boolean isRecovery = questionService.recoveryDeleted(ids);
        if (isRecovery) {
            return R.ok().message("恢复成功");
        }
        return R.error().message("恢复失败");
    }

    @ApiOperation("获得所有题目")
    @GetMapping("getAll")
    public R getAll(){
        List<Question> questions = questionService.list(null);
        return R.ok().data("questions",questions);
    }

}

